package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.QuitClass;

@Repository
public interface QuitClassDao extends JpaRepository<QuitClass, Integer> {

    @Transactional
    @Modifying
    Integer deleteByQuitId(Integer quitClassId);

    List<QuitClass> findByQuitId(Integer quitClassId);

    @Transactional
    @Modifying
    Integer deleteByQuitIdIn(List<Integer> listId);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,qc.* \r\n" +
            "from users u INNER JOIN quit_class qc ON u.user_Id = qc.user_Id")
    List<Map<String, Object>> queryAllQuitClassAndUsersInfo();

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,qc.* \r\n" +
            "from users u INNER JOIN quit_class qc ON u.user_Id = qc.user_Id\r\n" +
            "where qc.originalClass LIKE CONCAT('%',:originalClass,'%')")
    List<Map<String, Object>> findByOriginalClassLike(@Param("originalClass") String originalClass);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,qc.* \r\n" +
            "from users u INNER JOIN quit_class qc ON u.user_Id = qc.user_Id\r\n" +
            "where qc.quitStatus LIKE CONCAT('%',:quitStatus,'%')")
    List<Map<String, Object>> findByQuitStatusLike(@Param("quitStatus") String quitStatus);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,qc.* \r\n" +
            "from users u INNER JOIN quit_class qc ON u.user_Id = qc.user_Id\r\n" +
            "where qc.quitReason LIKE CONCAT('%',:quitReason,'%')")
    List<Map<String, Object>> findByQuitReasonLike(@Param("quitReason") String quitReason);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,qc.* \r\n" +
            "from users u INNER JOIN quit_class qc ON u.user_Id = qc.user_Id\r\n" +
            "where u.account LIKE CONCAT('%',:userName,'%')")
    List<Map<String, Object>> findByUserNameLike(@Param("userName") String userName);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,qc.* \r\n" +
            "from users u INNER JOIN quit_class qc ON u.user_Id = qc.user_Id\r\n" +
            "where u.nickName LIKE CONCAT('%',:nickName,'%')")
    List<Map<String, Object>> findByNickNameLike(@Param("nickName") String nickName);

    @Query(nativeQuery = true, value = "SELECT u.user_Id AS userId,u.account AS userName,u.nickName,qc.* \r\n" +
            "FROM users u INNER JOIN quit_class qc ON u.user_Id = qc.user_Id\r\n" +
            "WHERE u.user_Id = :userId")
    List<Map<String, Object>> selectQuitClassByUserId(@Param("userId") Integer userId);

}
